//
//  SiteManager.m
//  We_Travel
//
//  Created by lanou3g on 15/12/22.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "SiteManager.h"
#import "CoreDataToolHelper.h"
#import "NH_DataDownloadTools.h"
#import "url.h"
#import "SiteRecomendModel.h"
#import "Site.h"

@implementation SiteManager
//单例
+ (instancetype)shareSiteManager{
    static SiteManager *site = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        site = [[SiteManager alloc]init];
    });
    return site;
}
//重写get方法
- (NSInteger)countryCount{
    if (_isNewPushCom) {
        return [[CoreDataToolHelper shareCoreDataTool]fetchCountry].count - 1;
    }else{
        return _countryCount;
    }
}
- (void)requireDataIsRecomend:(BOOL)recomen RecomendSiteID:(NSInteger)siteID{
    if (recomen) {
        [self requireRecomend:siteID];
    }else{
        [self requireData];
    }
}
- (void)requireRecomend:(NSInteger)siteID{
    [NH_DataDownloadTools downloadDataWithURL:[NSString stringWithFormat:HT_SiteRecomendUrl,siteID] andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
        NSDictionary *diction = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *dictionary = diction[@"mtu_list"];
        self.mornArray = [NSMutableArray array];
        self.evenArray = [NSMutableArray array];
        self.afteArray = [NSMutableArray array];
        NSArray *array1 = dictionary[@"night"];
        NSArray *array2 = dictionary[@"am"];
        NSArray *array3 = dictionary[@"pm"];
        for (NSDictionary *dic in array1) {
            SiteRecomendModel *model = [[SiteRecomendModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.evenArray addObject:model];
        }
        for (NSDictionary *dic in array2){
            SiteRecomendModel *model = [[SiteRecomendModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.mornArray addObject:model];
        }
        for (NSDictionary *dic in array3) {
            SiteRecomendModel *model = [[SiteRecomendModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.afteArray addObject:model];
        }
        if (self.resultBlock) {
            self.resultBlock();
        }
    }];

}
- (void)requireData{
    NSArray *sites = [[CoreDataToolHelper shareCoreDataTool]fetchSiteTime:@"am" SiteDay:self.dayCountCurrent AreaCount:self.areaCount CountryCount:self.countryCount];
    self.mornArray = [NSMutableArray array];
    for (Site *s in sites) {
        [NH_DataDownloadTools downloadDataWithURL:[NSString stringWithFormat:HT_DetialSiteUrl,[s.siteID integerValue]] andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            SiteRecomendModel *model = [[SiteRecomendModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.mornArray addObject:model];
            if (self.resultBlock) {
                self.resultBlock();
            }
        }];
    }
    NSArray *sites2 = [[CoreDataToolHelper shareCoreDataTool]fetchSiteTime:@"pm" SiteDay:[SiteManager shareSiteManager].dayCountCurrent AreaCount:[SiteManager shareSiteManager].areaCount CountryCount:[SiteManager shareSiteManager].countryCount];
    self.afteArray = [NSMutableArray array];
    for (Site *s in sites2) {
        [NH_DataDownloadTools downloadDataWithURL:[NSString stringWithFormat:HT_DetialSiteUrl,[s.siteID integerValue]] andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            SiteRecomendModel *model = [[SiteRecomendModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.afteArray addObject:model];
            if (self.resultBlock) {
                self.resultBlock();
            }
        }];
    }
    NSArray *sites3 = [[CoreDataToolHelper shareCoreDataTool]fetchSiteTime:@"night" SiteDay:[SiteManager shareSiteManager].dayCountCurrent AreaCount:[SiteManager shareSiteManager].areaCount CountryCount:[SiteManager shareSiteManager].countryCount];
    self.evenArray = [NSMutableArray array];
    for (Site *s in sites3) {
        [NH_DataDownloadTools downloadDataWithURL:[NSString stringWithFormat:HT_DetialSiteUrl,[s.siteID integerValue]] andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            SiteRecomendModel *model = [[SiteRecomendModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.evenArray addObject:model];
            if (self.resultBlock) {
                self.resultBlock();
            }
        }];
    }
}
@end
